require("../../model/douyinModel.js");

Page({
    data: {
        colorArrF: [ "black", "whitesmoke", "red", "orange", "yellow" ],
        colorArrS: [ "green", "blue", "cyan", "purple", "gray" ],
        currentColor: "black",
        chooseImagesArr: [ "" ],
        textToPrint: "此证件只用于办理XX业务，他用无效",
        canProduce: !0,
        rotate: "45",
        alpha: 50,
        fontSize: 14
    },
    onLoad: function(e) {},
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    toBuildTheWaterMarkPic: function() {
        wx.chooseImage({
            count: 1,
            success: function(e) {
                var o = e.tempFilePaths;
                wx.navigateTo({
                    url: "/pages/waterMark/waterMark?imageUrl=" + o
                });
            }
        });
    },
    textFinish: function(e) {
        console.log(e);
        var o = e.detail.value;
        this.data.chooseImagesArr.length > 1 && o.length > 0 ? this.setData({
            canProduce: !1,
            textToPrint: o
        }) : this.setData({
            canProduce: !0,
            textToPrint: o
        });
    },
    chooseColorF: function(e) {
        console.log(e);
        var o = e.currentTarget.id;
        this.setData({
            currentColor: o
        });
    },
    chooseColorS: function(e) {
        var o = e.currentTarget.id;
        this.setData({
            currentColor: o
        });
    },
    sliderChange: function(e) {
        console.log(e);
        var o = e.detail.value;
        this.setData({
            rotate: o
        });
    },
    alphaSliderChange: function(e) {
        console.log(e);
        var o = e.detail.value;
        this.setData({
            alpha: o
        });
    },
    chooseImageTap: function() {
        var e = this, o = e.data.chooseImagesArr, t = 1;
        t = 1 == o ? 4 : 5 - o.length, wx.chooseImage({
            count: t,
            sizeType: [ "original", "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(t) {
                for (var a = t.tempFilePaths, n = 0; n < a.length; n++) {
                    var r = a[n];
                    o.unshift(r);
                }
                o.length > 4 && o.pop(), e.setData({
                    chooseImagesArr: o
                }), "" != e.data.textToPrint && e.setData({
                    canProduce: !1
                });
            }
        });
    },
    previewImagesTap: function(e) {
        var o = e.currentTarget.id;
        wx.previewImage({
            urls: this.data.chooseImagesArr,
            current: this.data.chooseImagesArr[o]
        });
    },
    deleteImages: function(e) {
        var o = e.currentTarget.id, t = this.data.chooseImagesArr;
        "" != t[o] && (this.isInArray(t, "") ? 3 != o && t.splice(o, 1) : (4 == t.length && t.push(""), 
        t.splice(o, 1)), this.setData({
            chooseImagesArr: t
        }), t.length <= 1 && this.setData({
            canProduce: !0
        }));
    },
    isInArray: function(e, o) {
        for (var t = 0; t < e.length; t++) if (o === e[t]) return !0;
        return !1;
    },
    toProducePic: function() {
        var e = this, o = e.data;
        wx.showLoading({
            title: "检测文本..."
        }), wx.cloud.callFunction({
            name: "checkText",
            data: {
                text: o.textToPrint
            },
            success: function(t) {
                if (wx.hideLoading(), 0 != t.result.errCode) wx.showModal({
                    title: "提示",
                    content: "输入水印文字不合规",
                    showCancel: !1
                }); else {
                    wx.showToast({
                        title: "文本合规"
                    }), console.log(o.chooseImagesArr);
                    for (var a = [], n = 0; n < o.chooseImagesArr.length; n++) {
                        var r = o.chooseImagesArr[n];
                        r.length > 0 && (a = a.concat(r));
                    }
                    console.log(a), e.checkImage(a, 0, function() {
                        var e = JSON.stringify(o.chooseImagesArr);
                        wx.navigateTo({
                            url: "/pages/waterMark/waterMark?imagesArr=" + e + "&textToPrint=" + o.textToPrint + "&rotate=" + o.rotate + "&color=" + o.currentColor + "&fontSize=" + o.fontSize + "&alpha=" + o.alpha
                        }), wx.hideLoading();
                    }, function(e) {
                        console.log(e), wx.showModal({
                            title: "提示",
                            content: "存在不合规图片",
                            showCancel: !1
                        }), wx.hideLoading();
                    });
                }
                console.log(t.result);
            },
            fail: function(e) {
                wx.hideLoading();
            }
        });
    },
    checkImage: function(e, o, t, a) {
        var n = this, r = e[o];
        if (o >= e.length) return wx.hideLoading(), void t();
        var c = "检测图片：" + (o += 1).toString() + "/" + e.length.toString();
        console.log(c), wx.showLoading({
            title: c
        }), n.checkImageCloud(r, function() {
            n.checkImage(e, o, t, a);
        }, function(e) {
            console.log(e), a(e);
        });
    },
    checkImageCloud: function(e, o, t) {
        console.log("checkImageCloud"), wx.getFileSystemManager().readFile({
            filePath: e,
            success: function(e) {
                console.log("start"), wx.cloud.callFunction({
                    name: "checkImage",
                    data: {
                        value: e.data
                    },
                    success: function(e) {
                        console.log(e), 0 == e.result.errCode ? o() : t(), console.log(e.result);
                    },
                    fail: function(e) {
                        console.log(e), t(e);
                    }
                });
            },
            fail: function(e) {
                console.log(e), t(e);
            }
        });
    },
    textSliderChange: function(e) {
        var o = e.detail.value;
        this.setData({
            fontSize: o
        });
    }
});